<?php

namespace app\index\Controller;
use think\Controller;
use think\Db;
use think\Request;
use think\Session;

class Ordermanagement extends Controller
{
   public function index()
   {
       if((Session::get('login_admin')) ||(Session::get('login_tel'))){

          if(Session::get('login_admin.id')){$user_id=Session::get('login_admin.id');} else{$user_id=Session::get('login_tel.id');}
           $list = Db::name('index_navigation')->select();
           //从数据库里查询友情链接
           $data = Db::table('lt_link')->select();
           $order = Db::table('lt_house_details')
               ->alias('a')
               ->join('lt_order_detials w','a.id = w.house_id')
               ->where('w.user_id',$user_id)
               ->select();

           if(is_mobile()){
               //手机端
               return view('ordermanagement/Mindex', [
                   'data'=>$data,
                   'info'=>$list,
                   'order'=>$order,
               ]);
           }else{
               //pc端
               return view('ordermanagement/index', [
                   'data'=>$data,
                   'info'=>$list,
                   'order'=>$order,
               ]);
           }
       }else{
           $this->error('亲，请登陆一下','/');
       }


   }


    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $result = Db::name('order_detials')->delete($id);
        if($result){
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = '订单删除成功';
        }else{
            $info['status'] = false;
            $info['id'] = $id;
            $info['info'] = '订单删除失败,请重试!';
        }
        return json($info);
    }

    public function update(Request $request, $id)
    {

        $info=[
            'order_status'=>'2',
        ];

        $result = Db::name('order_detials')->where('id',$id)->update($info);

        //订单获支付得积分---积分计算为每10块获得1积分
        $order = Db::name('order_detials')->where('id',$id)->find();
         if(session('login_admin')['id']){$lid=session('login_admin')['id'];}else{$lid=session('login_tel')['id'];}
        $orders = [
            'jifen'=>floor($order['total_price']/10),
            'lid'=>$lid,
            'oid'=>$id,
            'jf_time'=>time()
        ];
        //dump($orders);

        if ($result) {
            Db::name('index_integral')->insert($orders);
            return $this->success('支付成功,当前获得积分为'.$orders['jifen']);
        } else {
            return $this->error('支付失败');
        }
        $list = Db::name('index_navigation')->select();
        //从数据库里查询友情链接
        $data = Db::table('lt_link')->select();
        $order = Db::table('lt_house_details')
            ->alias('a')
            ->join('lt_order_detials w','a.id = w.house_id')
            ->where('w.id',$id)
            ->find();


        return view('ordermanagement/index', [
            'data'=>$data,
            'info'=>$list,
            'order'=>$order,
        ]);
    }


}